package cn.jx.asc.socket;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.json.JSONException;
import org.json.JSONObject;

public class InstructAndResult {
    private static int instructID = 10000;
    private static HashMap<Integer, List<Instruct>> instructs = new HashMap<Integer, List<Instruct>>(); 	//指令集
    private static HashMap<Integer, Result> results = new HashMap<Integer, Result>();	//结果集


    //判断是否有对应id的指令
    public static boolean hasInstructs(int id) {
        boolean has = instructs.containsKey(id);
        return has;
    }

    //获取指令
    public static List<Instruct> getInstructs(int id) {
        return instructs.get(id);
    }

    //删除指令
    public static void deleteInstructs(int id) {
        instructs.remove(id);
    }

    //发送指令
    public static int sendInstructs(int id,JSONObject insFromServer) {
        Set s = instructs.keySet();

        //id自动增加
        if(instructID<99999) {
            instructID++;
        }else {
            instructID = 10000;
        }

        //新建指令
        Instruct ins = new  Instruct(instructID,insFromServer);

        //如果指令集中存在该终端未发送的指令,则将指令插入原list
        if(instructs.containsKey(id)) {
            List instructList = instructs.get(id);
            instructList.add(ins);
            instructs.put(id, instructList);
        }else {
            //如果指令集中不存在该终端未发送的指令，则新建一个list插入
            List instructList = new ArrayList<>();
            instructList.add(ins);
            instructs.put(id, instructList);
        }
        return instructID;
    }

    //获取结果
    public static JSONObject getResult(int id) {
        JSONObject resultJson = null;

        int i = 0;
        while(i<6){
            if(!results.containsKey(id)) {
                try {
                    Thread.sleep(1000);
                    //System.out.println("---休眠1秒---");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            i++;
        }


        if(results.containsKey(id)) {
            Result re = results.get(id);

            if(re!=null) {
                resultJson = re.getResult();
            }else {
                resultJson = new JSONObject();
                try {
                    resultJson.put("flags", -1);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            //将结果删除
            results.remove(id);
        }else{
            resultJson = new JSONObject();
            //System.out.println("不存在对应id的result"+id);
            try {
                resultJson.put("flags", -1);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return resultJson;
    }

    //插入结果
    public static boolean insertResult(int id,JSONObject resultJson) {
        results.put(id, new Result(resultJson));
        return true;
    }

/*
	//监听指令集
	public static synchronized void monitorTnstrucAndResult() {
		while(true){
			//获取当前时间
			long timeNow = new Date().getTime();
			Set <Integer> setInstruct = instructs.keySet();
			Iterator iteratorInstruct=setInstruct.iterator();

			//对整个map循环判断
			while(iteratorInstruct.hasNext()){
				int id = (int) iteratorInstruct.next();
				List list = instructs.get(id);
				if(list == null) {
					iteratorInstruct.remove();
					continue;
				}
				Iterator itList = list.iterator();

				//对某个id的指令循环判断
				while(itList.hasNext()){
					Instruct inst = (Instruct) itList.next();

					//指令大于五秒未发送，删除该指令
					if(inst.getTime()-timeNow>1000*5){
						itList.remove();
						System.out.println("---删除了一个指令---");

					}
				}

				//指令集没有指令，移除该指令集
				if(list.isEmpty()){
					iteratorInstruct.remove();
					results.remove(id);
				}

			}

			//监听结果集
			Set<Integer> setResult = results.keySet();
			Iterator iteratorResult = setResult.iterator();
			while(iteratorResult.hasNext()){
				int resultID = (int) iteratorResult.next();

				//超过5秒钟没有取结果，则移除该结果
				if(results.get(resultID).getTime()-timeNow>1000*5){
					iteratorResult.remove();
					//results.remove(resultID);
				}
			}


		}
	}
*/


}
